<?php

namespace app\admin\service;

use app\admin\model\BoxGame;
use app\admin\model\BoxGameDataAllLog;
use app\admin\model\BoxGameMemberLostLog;
use app\admin\model\BoxGameMemberLtvLog;
use app\admin\model\BoxGameMemberPayRemainLog;
use app\admin\model\BoxGameMemberRemainLog;
use app\admin\model\BoxGamePayFirstLog;
use app\admin\validate\BoxBanConfigValidate;
use DateTime;

class BoxGameDataAllService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxGameDataAllLog();
    }

    public function getList()
    {
        $param = request()->param();
        $map   = [];
        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = $this->model
            ->where($map)
            ->order('day_time', 'desc')
            ->order('box_game_id', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();
        foreach ($list as $key => &$val) {
            $game_name        = (new BoxGame())->where('id', '=', $val['box_game_id'])->field('game_name')->find();
            $val['game_name'] = $game_name['game_name'];
            $date             = new DateTime($val['day_time']);
            $val['day_time']  = $date->format('Y-m-d'); // 输出：2023-12-11
            if (!empty($val['all_pay_amount']) && $val['all_account_num']) {
                $val['arpu'] = sprintf("%.2f", $val['all_pay_amount'] / $val['all_account_num']);
            } else {
                $val['arpu'] = 0;
            }

            if (!empty($val['all_pay_amount']) && $val['all_pay_account_num']) {
                $val['arppu'] = sprintf("%.2f", $val['all_pay_amount'] / $val['all_pay_account_num']);
            } else {
                $val['arppu'] = 0;
            }
        }
        //获取数据总数
        $count = $this->model->where($map)->count();
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

    public function getMemberRemainList(): array
    {
        $param = request()->param();
        $map   = [];
        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = (new BoxGameMemberRemainLog)
            ->where($map)
            ->order('day_time', 'desc')
            ->order('box_game_id', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();
        foreach ($list as $key => &$val) {
            $game_name        = (new BoxGame())->where('id', '=', $val['box_game_id'])->field('game_name')->find();
            $val['game_name'] = $game_name['game_name'];
            $date             = new DateTime($val['day_time']);
            $val['day_time']  = $date->format('Y-m-d'); // 输出：2023-12-11
            if (!empty($val['account_num'])) {
                for ($i = 2; $i <= 60; $i++) {
                    $val['d' . $i] = round($val['d' . $i] / $val['account_num'] * 100, 2);
                }
            }

        }
        //获取数据总数
        $count = (new BoxGameMemberRemainLog)->where($map)->count();
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

    public function getMemberPayRemainList(): array
    {
        $param = request()->param();
        $map   = [];
        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = (new BoxGameMemberPayRemainLog)
            ->where($map)
            ->order('day_time', 'desc')
            ->order('box_game_id', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();
        foreach ($list as $key => &$val) {
            $game_name        = (new BoxGame())->where('id', '=', $val['box_game_id'])->field('game_name')->find();
            $val['game_name'] = $game_name['game_name'];
            $date             = new DateTime($val['day_time']);
            $val['day_time']  = $date->format('Y-m-d'); // 输出：2023-12-11
            if (!empty($val['account_pay_num'])) {
                for ($i = 2; $i <= 60; $i++) {
                    $val['d' . $i] = round($val['d' . $i] / $val['account_pay_num'] * 100, 2);
                }
            }
        }
        //获取数据总数
        $count = (new BoxGameMemberPayRemainLog)->where($map)->count();
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

    public function getMemberPayFirstList(): array
    {
        $param = request()->param();
        $map   = [];
        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = (new BoxGamePayFirstLog)
            ->where($map)
            ->order('day_time', 'desc')
            ->order('box_game_id', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();
        foreach ($list as $key => &$val) {
            $game_name        = (new BoxGame())->where('id', '=', $val['box_game_id'])->field('game_name')->find();
            $val['game_name'] = $game_name['game_name'];
            $date             = new DateTime($val['day_time']);
            $val['day_time']  = $date->format('Y-m-d'); // 输出：2023-12-11
            if (!empty($val['account_num'])) {
                for ($i = 2; $i <= 60; $i++) {
//                    $val['d' . $i] = sprintf('%.2f',round($val['d' . $i] / $val['pay_account_num']*100, 2));
                    $val['d' . $i] = round($val['d' . $i] / $val['account_num'] * 100, 2);
                }
            }
        }
        //获取数据总数
        $count = (new BoxGamePayFirstLog)->where($map)->count();
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

    public function getMemberLtvList(): array
    {
        $param = request()->param();
        $map   = [];
        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = (new BoxGameMemberLtvLog())
            ->where($map)
            ->order('day_time', 'desc')
            ->order('box_game_id', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();
        foreach ($list as $key => &$val) {
            $game_name        = (new BoxGame())->where('id', '=', $val['box_game_id'])->field('game_name')->find();
            $val['game_name'] = $game_name['game_name'];
            $date             = new DateTime($val['day_time']);
            $val['day_time']  = $date->format('Y-m-d'); // 输出：2023-12-11
        }
        //获取数据总数
        $count = (new BoxGameMemberLtvLog)->where($map)->count();
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

    public function getMemberLostList(): array
    {
        $param = request()->param();
        $map   = [];
        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = (new BoxGameMemberLostLog())
            ->where($map)
            ->order('day_time', 'desc')
            ->order('box_game_id', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();
        foreach ($list as $key => &$val) {
            $game_name        = (new BoxGame())->where('id', '=', $val['box_game_id'])->field('game_name')->find();
            $val['game_name'] = $game_name['game_name'];
            $date             = new DateTime($val['day_time']);
            $val['day_time']  = $date->format('Y-m-d'); // 输出：2023-12-11
            switch ($val['day_type']) {
                case 1:
                    $val['day_type'] = '1~7天';
                    break;
                case 2:
                    $val['day_type'] = '7~14天';
                    break;
                case 3:
                    $val['day_type'] = '14~30天';
                    break;
                case 4:
                    $val['day_type'] = '30天以上';
                    break;
                default:
                    $val['day_type'] = '---';
            }
        }
        //获取数据总数
        $count = (new BoxGameMemberLostLog)->where($map)->count();
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

}